<template>
  <div class="article-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑导航 -->
        <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb> -->
        <my-breadcrumb>内容管理</my-breadcrumb>
      </div>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="状态">
      <!-- label用来决定选择项被选中后，表示的值 -->
      <el-radio-group v-model="form.status">
      <el-radio :label="null">全部</el-radio>
      <el-radio :label="0">草稿</el-radio>
      <el-radio :label="1">待审核</el-radio>
      <el-radio :label="2">审核通过</el-radio>
      <el-radio :label="3">审核失败</el-radio>
      <el-radio :label="4">已删除</el-radio>
    </el-radio-group>
  </el-form-item>
  <my-channels v-model="form.channelId"></my-channels>
  <!-- <el-form-item label="频道"> -->
    <!-- clearable表示可清空选项 -->
    <!-- <el-select v-model="form.channelId" clearable placeholder="请选择频道">
      <el-option :label="channel.name"
      :value="channel.id"
      v-for="channel in channels" :key="channel.id"
      ></el-option>
    </el-select>
  </el-form-item> -->
  <el-form-item label="日期">
   <el-date-picker
      v-model="form.date"
      type="daterange"
      value-format="yyyy-MM-dd"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
    </el-date-picker>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="hQuery" :loading="loading">查询</el-button>
    <el-button>取消</el-button>
  </el-form-item>
  </el-form>
  </el-card>
  <!-- 下部表格区域 -->
    <el-card style="margin-top:20px">
      <div slot="header" class="clearfix">
       根据筛选条件查询到{{total_count}}条数据，当前是第{{curPage}}页
      </div>
        <el-table
        v-loading='loading '
      :data="articles"
      style="width: 100%"
      >
      <el-table-column
        label="封面">
        <template slot-scope="scope">
          <!-- 图片 -->
          <!-- <div>{{scope.row.cover.images[0]}}</div> -->
          <el-image :src="scope.row.cover.images[0]"
          style="width:150px;height:100px"
          lazy>
          <div slot="placeholder" class="image-slot">
            加载中<span class="dot">...</span>
           </div>
        </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="180">
      </el-table-column>
      <el-table-column
        label="状态"
        width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status===0">草稿</el-tag>
          <el-tag v-else-if="scope.row.status===1" type="info">待审核</el-tag>
          <el-tag v-else-if="scope.row.status===2" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status===3" type="warning">审核失败</el-tag>
          <el-tag v-else-if="scope.row.status===4" type="danger">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="pubdate"
        label="发布时间"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
        <el-button
          size="mini" icon="el-icon-edit" circle type="primary"
          @click="hEdit(scope.row)"></el-button>
        <el-button
          size="mini"
          type="danger" icon="el-icon-delete" circle
          @click="hDelete(scope.row)"></el-button>
      </template>
      </el-table-column>
    </el-table>
    <!-- 分页,total总数，默认每页10条,current-change点击页码触发-->
    <el-pagination
      :disabled="loading"
      style="margin-top:10px"
      @current-change="hPageChange"
      background
      layout="prev, pager, next"
      :page-size="per_page"
      :total="total_count">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
import MyChannels from '@/components/MyChannelsVmodel'
import MyBreadcrumb from '../../components/MyBreadcrumb'
import { getArticles, deleteArticle } from '../../api/article.js'
export default {
  name: 'ArticleIndex',
  props: {},
  data () {
    return {
      loading: false,
      curPage: 1, // 当前要查询的页码
      total_count: 0, // 当前能查到的总条数
      per_page: 10,
      form: {
        channelId: null, // 下拉列表选中的值
        status: null, // 查询文章状态，默认null
        date: null, // 查询日期区间，默认为null
        delivery: false
      },
      // channels: [], // 保存获取频道
      articles: [] // 文章列表
    }
  },
  components: {
    MyBreadcrumb,
    MyChannels
  },
  computed: {},
  created () {
    this.loadArticles()
    // this.loadChannels()
  },
  methods: {
    // 读取文章列表
    loadArticles () {
      // 根据接口，补充begin_pubdate，end_pubdate值，如果用户选择了日期范围，传入即可，
      // 如没有，传null——axios会过滤掉，this.form.date为null，整个式子是null
      const beginPubdate = this.form.date && this.form.date[0]
      const endPubdate = this.form.date && this.form.date[1]
      // let channelId = null
      // if (this.form.channelId) {
      //   channelId = this.form.channelID
      // }
      this.loading = true // 查询加载状态
      getArticles({
        channel_id: this.form.channelId || null,
        // channel_id: channelId,
        status: this.form.status,
        begin_pubdate: beginPubdate,
        end_pubdate: endPubdate,
        page: this.curPage
      }).then(res => {
        // console.log(res)
        this.articles = res.data.data.results
        // 保存总数
        this.total_count = res.data.data.total_count
        this.loading = false // 关闭加载状态
      }).catch(err => {
        console.log(err)
        // alert('错误')
        this.$message({
          message: '查询失败',
          type: 'error'
        })
        this.loading = false // 关闭加载状态
      })
    },
    // loadChannels () {
    //   getArticleChannels().then(res => {
    //     // console.log('获取频道数据', res)
    //     this.channels = res.data.data.channels
    //   })
    // },
    hEdit (row) {
      // 路由跳转到编辑页，传入ID
      // console.log('编辑功能', row)
      const id = row.id.toString()
      this.$router.push('/editarticle/' + id)
    },
    hDelete (row) {
      // console.log('要删除的数据', row)
      const id = row.id.toString()
      // console.log('要删除的数据shi', id)
      this.$confirm('删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 根据ID删除数据
        deleteArticle(id).then(res => {
          console.log(res)
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.loadArticles()
        }).catch(err => {
          console.log(err)
          this.$message({
            message: '删除失败',
            type: 'error'
          })
        })
      }).catch(() => {
        // 用户取消了
      })
    },
    hPageChange (curPage) {
      // console.log(curPage)
      // 更新当前页码
      this.curPage = curPage
      // 再发请求
      this.loadArticles()
    },
    hQuery () {
      // console.log(this.form.date)
      // 再次调用查询方法
      this.loadArticles()
      // 用户点击查询，重置页码
      this.curPage = 1
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
